/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.os.rh.telas;

import br.os.rh.disciplina.Disciplina;
import br.os.rh.disciplina.DisciplinaDAO;
import br.os.rh.funcionario.Funcionario;
import br.os.rh.funcionario.FuncionarioDAO;
import br.os.rh.funcionario.FuncionarioTableModel;
import br.os.rh.lotacao.Lotacao;
import br.os.rh.lotacao.LotacaoDAO;
import br.os.rh.lotacao.LotacaoTableModel;
import br.os.rh.lotacaodisciplinahorario.LotacaoDisciplinaHorario;
import br.os.rh.lotacaodisciplinahorario.LotacaoDisciplinaHorarioDAO;
import br.os.rh.lotacaodiscplinas.LotacaoDisciplina;
import br.os.rh.lotacaodiscplinas.LotacaoDisciplinaDAO;
import br.os.rh.lotacaodiscplinas.LotacaoDisciplinaTableModel;
import br.os.rh.salario.Salario;
import br.os.rh.salario.SalarioDAO;
import br.os.rh.util.Ativo;
import groovy.lang.Closure;
import groovy.swing.SwingBuilder;
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author 'Pedro
 */
public class TelaLotacao extends javax.swing.JDialog {

    private Lotacao lotacao;
    private List<LotacaoDisciplina> disciplinas;

    /**
     * Creates new form TelaLotacao
     */
    public TelaLotacao() {
        initComponents();
        setLocationRelativeTo(null);
        setModal(true);
        limpaCampos();
        preencheTabelaDisciplinas();
    }

    public void calculaHoras() {
        int soma = 0;
        List<LotacaoDisciplina> lista = new ArrayList(new HashSet(disciplinas));
        for (int i = 0; i < lista.size(); i++) {
            for (int j = 0; j < lista.get(i).getSdh().size(); j++) {
                soma += lista.get(i).getSdh().get(j).getHorario().getQtdHora();
            }
        }
        ltotal.setText("Total: " + String.valueOf(soma * 20));
        lSemanais1.setText("Semanal: " + String.valueOf(soma));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        tfFuncionario = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDisciplinas = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        ltotal = new javax.swing.JLabel();
        lSemanais1 = new javax.swing.JLabel();
        lblFoto = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel4.setBackground(new java.awt.Color(153, 204, 255));

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Nome:*");
        jPanel5.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        jButton7.setText("...");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 50, 35, -1));

        tfFuncionario.setEnabled(false);
        tfFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfFuncionarioActionPerformed(evt);
            }
        });
        jPanel5.add(tfFuncionario, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 320, -1));

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/novo_1.png"))); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 320, -1, -1));

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/SA.png"))); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 320, -1, 40));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/procurar_1.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 320, -1, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/save_1.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 320, -1, -1));

        tbDisciplinas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDisciplinas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDisciplinasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDisciplinas);

        jPanel5.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 590, 190));

        jLabel3.setFont(new java.awt.Font("SansSerif", 0, 12)); // NOI18N
        jLabel3.setText("Disciplinas:*");
        jPanel5.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, -1));

        jButton2.setText("+");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 120, 40, -1));

        jButton6.setText("-");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 150, 40, -1));

        jLabel4.setText("** CLique duas vezes no registro para obter informações extras");
        jPanel5.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 310, -1, -1));

        ltotal.setText("Total:");
        jPanel5.add(ltotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 270, -1, -1));

        lSemanais1.setText("Semanal:");
        jPanel5.add(lSemanais1, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 290, -1, -1));

        lblFoto.setBackground(new java.awt.Color(51, 51, 51));
        lblFoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFoto.setText("* Foto");
        lblFoto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblFoto.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblFoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblFotoMouseClicked(evt);
            }
        });
        jPanel5.add(lblFoto, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 10, 90, 100));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("LOTAÇÃO");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("jLabel2");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 702, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(2, 2, 2))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        FuncionarioDAO dao = new FuncionarioDAO();
        List<Funcionario> lista = dao.listarProfessorAtivo();
        FuncionarioTableModel stm = new FuncionarioTableModel(lista);
        Object o = TelaPesquisa.exibeTela(stm, "Funcionário");
        Funcionario f;
        if (o != null) {
            limpaCampos();
            f = dao.pesquisaId(Integer.valueOf(String.valueOf(o)));
            LotacaoDAO lDAO = new LotacaoDAO();

            if (lDAO.pesquisaFuncionarioPeriodo(f, Ativo.getPeriodo()).size() > 0) {
                lotacao = lDAO.pesquisaFuncionarioPeriodo(f, Ativo.getPeriodo()).get(0);
                tfFuncionario.setText(lotacao.getProfessor().getNome());

                LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
                disciplinas = new ArrayList(new HashSet(sDAO.pesquisaLotacao(lotacao)));
                preencheTabelaDisciplinas();
                calculaHoras();
                carregarFoto(lotacao.getProfessor().getCaminhoFoto());
            } else if (f.isAtivo()) {
                lotacao.setProfessor(f);
                tfFuncionario.setText(f.getNome());
                carregarFoto(f.getCaminhoFoto());
            } else {
                JOptionPane.showMessageDialog(rootPane, "Funcionário Não está Ativo!");
            }
        }

    }//GEN-LAST:event_jButton7ActionPerformed

    private void carregarFoto(String path) {
        String caminhoFoto = path;

        if (!caminhoFoto.equals("")) {
            ImageIcon imagem = new ImageIcon(caminhoFoto);

            Image img = imagem.getImage().getScaledInstance(lblFoto.getWidth() + 2, lblFoto.getHeight() + 2, Image.SCALE_DEFAULT);
            lblFoto.setIcon(new ImageIcon(img));
        }
    }

    private void tfFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfFuncionarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfFuncionarioActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        limpaCampos();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        LotacaoDAO dao = new LotacaoDAO();
        List<Lotacao> lotacoes = (dao.listarPorCoordenador());

        LotacaoTableModel stm = new LotacaoTableModel(lotacoes);

        Object o = TelaPesquisa.exibeTela(stm, "Lotacoes");

        LotacaoDAO lDAO = new LotacaoDAO();

        lotacao = lDAO.checkExists("id", Integer.valueOf(String.valueOf(o))).get(0);

        LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
        disciplinas = new ArrayList(new HashSet(sDAO.pesquisaLotacao(lotacao)));
        preencheTabelaDisciplinas();
        tfFuncionario.setText(lotacao.getProfessor().getNome());

//        carregarFoto(lotacao.getProfessor().getCaminhoFoto());
        calculaHoras();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        LotacaoDAO dao = new LotacaoDAO();
        if (!tfFuncionario.getText().equals("") && disciplinas.size() > 0) {
            LotacaoDAO lDao = new LotacaoDAO();
            lotacao.setPeriodo(Ativo.getPeriodo());
            lDao.salvar(lotacao);

            //salva as disciplinas
            LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
            for (int i = 0; i < disciplinas.size(); i++) {
                disciplinas.get(i).setLotacao(lotacao);
                sDAO.salvar(disciplinas.get(i));
                for (int j = 0; j < disciplinas.get(i).getSdh().size(); j++) {
                    LotacaoDisciplinaHorarioDAO sdhDAO = new LotacaoDisciplinaHorarioDAO();
                    disciplinas.get(i).getSdh().get(j).setSalarioDisciplina(disciplinas.get(i));
                    sdhDAO.salvar(disciplinas.get(i).getSdh().get(j));
                }
            }

            limpaCampos();
            JOptionPane.showMessageDialog(rootPane, "Lotação Salva Com Sucesso!");
        } else {
            JOptionPane.showMessageDialog(rootPane, "Preencha os Campos!");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (tfFuncionario.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Informe o Funcionário!");
        } else {
            DisciplinaDAO dDAO = new DisciplinaDAO();
            Disciplina d = new Disciplina();
            LotacaoDisciplina sd = new LotacaoDisciplina();
            sd = TelaLotacaoDisciplina.chamaTela(disciplinas);

            if (sd != null) {
//                d = sd.getDisciplina();
                d = dDAO.pesquisaId(Integer.valueOf(String.valueOf(sd.getDisciplina().getId())));
                if (!containsDisciplina(d)) {
                    LotacaoDAO sDAO = new LotacaoDAO();
                    List<Lotacao> retorno = sDAO.pesquisaDisciplinaPeriodo(d, Ativo.getPeriodo());
                    if (retorno.size() > 0) {
                        if (JOptionPane.showConfirmDialog(rootPane, "Disciplina cadastrada para outros professores \n"
                                + " no período indicado, deseja continuar?"
                                + retorno.toString(), "OSRH", JOptionPane.YES_NO_OPTION,
                                JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
                            //SalarioDisciplina sd = new SalarioDisciplina();
//                            sd.setDisciplina(d);
                            disciplinas.add(sd);
                            preencheTabelaDisciplinas();
                            calculaHoras();
                        }
                    } else {
//                        SalarioDisciplina sd = new SalarioDisciplina();
//                        sd.setDisciplina(d);
                        disciplinas.add(sd);
                        preencheTabelaDisciplinas();
                        calculaHoras();
                    }
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Esta disciplina já está na lista!");
                }
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        int row = tbDisciplinas.getSelectedRow();
        if (row > -1) { //então tem ítem selecionado
            Object o = tbDisciplinas.getValueAt(row, 0);
            int id = Integer.parseInt(String.valueOf(o));
            if (id > 0) {
                LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
                LotacaoDisciplina sd = new LotacaoDisciplina();
                sd = sDAO.checkExists("id", id).get(0);
                //excluir disciplina

                if (verificaSePodeExcluir(sd)) {

                    if (JOptionPane.showConfirmDialog(rootPane, "Deseja excluir a disciplina: "
                            + sd.getDisciplina().getDescricao(), "Confirmação", JOptionPane.YES_NO_OPTION,
                            JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {

                        LotacaoDisciplinaHorarioDAO ldhDAO = new LotacaoDisciplinaHorarioDAO();
                        //remove os horários
                        for (int i = 0; i < sd.getSdh().size(); i++) {
                            ldhDAO.remover(sd.getSdh().get(i));
                        }
                        sd = sDAO.checkExists("id", sd.getId()).get(0);
                        //remove a disciplina
                        sDAO.remover(sd);
                        //atualiza a lista de disciplinas
                        removeDisciplina(sd);
                        preencheTabelaDisciplinas();
                        JOptionPane.showMessageDialog(rootPane, "Disciplina excluída com sucesso!",
                                "INFO", JOptionPane.INFORMATION_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Você não possui permissão para excluir a disciplina!",
                            "ERRO", JOptionPane.ERROR_MESSAGE);
                }

                calculaHoras();
            } else {
                JOptionPane.showMessageDialog(rootPane, "Disciplina não salva, complete o cadastro ou cancele-o!",
                        "ERRO", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Selecione o Ítem!",
                    "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    public boolean verificaSePodeExcluir(LotacaoDisciplina lDisciplina) {
        LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
        List<LotacaoDisciplina> lista = sDAO.pesquisaPorCurso();
        boolean retorno = true;
        for (LotacaoDisciplina lotacaoDisciplina : lista) {
            if (lotacaoDisciplina.getDisciplina().getId() == lDisciplina.getDisciplina().getId()) {
                return true;
            } else {
                retorno = false;
            }
        }
        return retorno;
    }

    private void tbDisciplinasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDisciplinasMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            int row = tbDisciplinas.getSelectedRow();
            if (row > -1) { //então tem ítem selecionado
                Object o = tbDisciplinas.getValueAt(row, 0);
                int id = Integer.parseInt(String.valueOf(o));
                if (id > 0) {
                    LotacaoDisciplinaDAO sDAO = new LotacaoDisciplinaDAO();
                    LotacaoDisciplina sd = new LotacaoDisciplina();
                    sd = sDAO.checkExists("id", id).get(0);
                    String texto = sd.getDisciplina().getDescricao() + " | " + sd.getDisciplina().getHoras() / 20 + " hrs semanais";
                    texto += "\n" + sd.getDisciplina().getSemestre().getCurso().getDescricao() + " | "
                            + sd.getDisciplina().getSemestre().getDescricao();
                    texto += "\n___________________________________________";
                    int soma = 0;
                    for (LotacaoDisciplinaHorario sdh : sd.getSdh()) {
                        texto += "\n" + sdh.getHorario().getHoraInicial() + " " + sdh.getHorario().getHoraFinal()
                                + " | " + sdh.getHorario().getDescricao() + " " + sdh.getHorario().getTurno().getDescricao() + " | " + sdh.getHorario().getQtdHora() + " hrs "
                                + " | " + sdh.getDiaSemana();
                        soma += sdh.getHorario().getQtdHora();
                    }
                    texto += "\n___________________________________________";
                    texto += "\nHoras Semanais Lotadas:" + soma;

                    TelaInfoExtra.chamaTela(texto);
                }
            }
        }
    }//GEN-LAST:event_tbDisciplinasMouseClicked

    private void lblFotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblFotoMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_lblFotoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaLotacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaLotacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaLotacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaLotacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaLotacao().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lSemanais1;
    private javax.swing.JLabel lblFoto;
    private javax.swing.JLabel ltotal;
    private javax.swing.JTable tbDisciplinas;
    private javax.swing.JTextField tfFuncionario;
    // End of variables declaration//GEN-END:variables

    private void limpaCampos() {
        lotacao = new Lotacao();
        jLabel2.setText("Período: " + Ativo.getPeriodo().getDescricao());
        tfFuncionario.setText("");
        disciplinas = new ArrayList<>();
        preencheTabelaDisciplinas();
        ltotal.setText("Total: ");
        lSemanais1.setText("Semanal: ");
        carregarFoto("");
        lblFoto.setIcon(null);

    }

    private void preencheTabelaDisciplinas() {
        LotacaoDisciplinaTableModel dtm = new LotacaoDisciplinaTableModel(disciplinas);
        tbDisciplinas.setModel(dtm);

    }

    private boolean containsDisciplina(Disciplina d) {
        for (int i = 0; i < disciplinas.size(); i++) {
            if (disciplinas.get(i).getDisciplina().equals(d)) {
                return true;
            }
        }
        return false;
    }

    private void removeDisciplina(LotacaoDisciplina sd) {
        for (int i = 0; i < disciplinas.size(); i++) {
            if (disciplinas.get(i).getId() == sd.getId()) {
                disciplinas.remove(i);
                return;
            }
        }
    }
}
